module mic_led (
    input clk,       // Clock
    input signed [5:0] mic1,
    input signed [5:0] mic2,
    input signed [5:0] mic3,
    input tVAD,
    output reg sk9822_ck,
    output reg sk9822_da,
    output reg [3:0] direction,
    output reg [3:0] direction2,
    output reg [3:0] direction3
  );


  parameter SD9822_NUM = 12;//串联灯珠数量

  parameter FRAME_LEN  = 32;//数据帧长度

  //起始帧
  parameter START_FRAME = 32'H00000000;
  //结束帧
  parameter END_FRAME   = 32'HFFFFFFFF;
  //数据帧
  parameter LED_LIGHT = 5'B01111; //全局亮度
  reg [23:0] data_rgb = 24'h000001; //具体颜色
  reg [23:0] data_rgb_white = 24'b00001000_00001000_00001000;//白色
  reg [23:0] data_rgb_none = 24'd0;//无色
  wire [31:0] data_frame = {3'b111,LED_LIGHT,data_rgb};

   //大致计算一下输入方向
    always @(posedge clk_slow) begin
        if(mic1  > 20)
            direction <= 4'd4;
        else if(mic1  < -20)
            direction <= 4'd10;
        else
            direction <= 4'd15;
    end
    always @(posedge clk_slow) begin
        if(mic2 > 20)
            direction2 <= 4'd0;
        else if(mic2 < -20)
            direction2 <= 4'd6;
        else
            direction2 <= 4'd15;
    end
    always @(posedge clk_slow) begin
        if(mic3 > 20)
            direction3 <= 4'd2;
        else if(mic3 < -20)
            direction3 <= 4'd8;
        else
            direction3 <= 4'd15;
    end
  //clk降频 大概 1M
  reg [23:0] clk_delay;
  wire clk_slow = clk_delay[4];
  always@(posedge clk) clk_delay <= clk_delay + 1;

  //发送状态机
  reg [31:0] send_frame;
  reg [6:0] send_frame_cnt = 0;
  reg [4:0] send_bit_cnt = 0;
  reg 	  send_bit;


  // assign direction=4'd5 ;

  always@(posedge clk_slow)
    if(!sk9822_ck)
    begin //上升沿发送
      sk9822_ck <= 1;
    end
    else
    begin     //下降沿取值
      sk9822_ck <= 0;
      sk9822_da <= send_frame[(FRAME_LEN - 1) - send_bit_cnt];
      send_bit_cnt <= send_bit_cnt + 1;

      if(send_bit_cnt == FRAME_LEN - 1)
        send_frame_cnt <= (send_frame_cnt < (SD9822_NUM + 1))? send_frame_cnt + 1 : 0;
    end

  //发送数据控制
  always@(*)
    if(send_frame_cnt == 0)
      send_frame = START_FRAME;
    else if(send_frame_cnt == (SD9822_NUM + 1))
      send_frame = END_FRAME;
    else
      send_frame = data_frame;

  //颜色转移状态机
  always@(posedge clk_slow)
  begin
    if ((send_frame_cnt==(direction+1'b1) || send_frame_cnt==(direction2+1'b1) || send_frame_cnt==(direction3+1'b1))&&tVAD)
      data_rgb <= data_rgb_white;
    else
      data_rgb = 24'h000000;
  end

endmodule
